/**
 * 
 */
package unseen.games.model;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

/**
 * 
 * @author PERRAUDIN Steven <sperraudin@neotilus.com>
 *
 * @date 4 juin 2010
 */
public class Wave {
	
	private float x;
	private float y;
	
	public float[] rgb	= new float[3];
		
	public int radius;
	
	private int _nrOfVertices = 0;
	
	// a raw buffer to hold indices allowing a reuse of points.
    private ShortBuffer _indexBuffer;
    
    // a raw buffer to hold the vertices
    private FloatBuffer _vertexBuffer;
    

	public Wave(float x, float y, float red, float blue, float green){
		
		this.x 		= x;
		this.y 		= y + 25.0f;
		
		this.rgb[0] = red;
		this.rgb[1] = blue;
		this.rgb[2] = green;
		
		this.radius = 10;		
		
		initView();
		
	}
	
	private void initView(){
				 
	float[] _coords = 	{ 
					x,				y,					0f,		// center
					x - radius,  	y - radius,			0f,		// (x1, y1, z1)
					x - radius,  	y + radius,			0f,		// (x2, y2, z2)
					x + radius, 	y + radius, 		0f,		// (x3, y3, z3) 
					x + radius, 	y - radius, 		0f,		// (x3, y3, z3) 
					x - radius, 	y - radius, 		0f,		// (x3, y3, z3) 
           			};
		
	 short[] indices = {0, 1, 2, 3, 4, 5};
		
        _nrOfVertices 	= _coords.length;
        
       
		
     // float has 4 bytes, coordinate * 4 bytes
        ByteBuffer vbb = ByteBuffer.allocateDirect(_coords.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        _vertexBuffer = vbb.asFloatBuffer();
		
     // short has 2 bytes, indices * 2 bytes
        ByteBuffer ibb = ByteBuffer.allocateDirect(_nrOfVertices * 2);
        ibb.order(ByteOrder.nativeOrder());
        _indexBuffer = ibb.asShortBuffer();
        
        _vertexBuffer.put(_coords);
        _indexBuffer.put(indices);
        
        _vertexBuffer.position(0);
        _indexBuffer.position(0);
		
	}
		
	public void animate(){
		_vertexBuffer.put(3, _vertexBuffer.get(3) 	- 1f);
		_vertexBuffer.put(4, _vertexBuffer.get(4) 	- 1f);
		_vertexBuffer.put(6, _vertexBuffer.get(6) 	- 1f);
		_vertexBuffer.put(7, _vertexBuffer.get(7) 	+ 1f);
		_vertexBuffer.put(9, _vertexBuffer.get(9) 	+ 1f);
		_vertexBuffer.put(10, _vertexBuffer.get(10) + 1f);
		_vertexBuffer.put(12, _vertexBuffer.get(12) + 1f);
		_vertexBuffer.put(13, _vertexBuffer.get(13) - 1f);
		_vertexBuffer.put(15, _vertexBuffer.get(15) - 1f);
		_vertexBuffer.put(16, _vertexBuffer.get(16) - 1f);
		
		radius++;
	}
	
	
	/**
	 * @return the _nrOfVertices
	 */
	public int getNrOfVertices() {
		return _nrOfVertices;
	}


	/**
	 * @param nrOfVertices the _nrOfVertices to set
	 */
	public void setNrOfVertices(int nrOfVertices) {
		_nrOfVertices = nrOfVertices;
	}


	/**
	 * @return the _indexBuffer
	 */
	public ShortBuffer getIndexBuffer() {
		return _indexBuffer;
	}


	/**
	 * @param indexBuffer the _indexBuffer to set
	 */
	public void setIndexBuffer(ShortBuffer indexBuffer) {
		_indexBuffer = indexBuffer;
	}


	/**
	 * @return the _vertexBuffer
	 */
	public FloatBuffer getVertexBuffer() {
		return _vertexBuffer;
	}


	/**
	 * @param vertexBuffer the _vertexBuffer to set
	 */
	public void setVertexBuffer(FloatBuffer vertexBuffer) {
		_vertexBuffer = vertexBuffer;
	}


	/**
	 * @return the radius
	 */
	public int getRadius() {
		return radius;
	}


	/**
	 * @param radius the radius to set
	 */
	public void setRadius(int radius) {
		this.radius = radius;
	}
	
	
}
